<?php

class FlagsController extends ApplicationController {
	protected $AppName = "flags";
	protected $UseApps = array(array("name" => "mysql"));
	
	function init(){
		return $this;
	}
	
	function updateFlags($flag_key, array $flags){
		$MySQL = $this->UseApps["mysql"];
		
		if(count($flags) > 0){
			foreach ($flags as $flagName => $flagValue){
				$CheckFlagAvalibility = $MySQL->query("SELECT * FROM {$MySQL->getPrefix()}flags WHERE flag_key = '$flag_key' AND flag_name = '$flagName'");
				
				if(mysql_num_rows($CheckFlagAvalibility) > 0){
					$Sql = $MySQL->generateUpdateQuery($MySQL->getPrefix()."flags", array("flag_value" => $flagValue), array("flag_key" => $flag_key, "flag_name" => "$flagName"));					
				}
				else {
					$Sql = "INSERT INTO {$MySQL->getPrefix()}flags (flag_key, flag_name, flag_value) VALUES('$flag_key', '$flagName', '$flagValue')";
				}
				
				$MySQL->query($Sql);
			}
			return true;
		}
		else {
			return false;
		}
	}
	
	function getFlags($flag_key){
		$MySQL = $this->UseApps["mysql"];
		
		$SelectFlags = $MySQL->query("SELECT * FROM {$MySQL->getPrefix()}flags WHERE flag_key = '$flag_key'");
		
		$Flags = array();
		
		while ($flag = mysql_fetch_assoc($SelectFlags)) {
			$Flags[$flag["flag_name"]] = $flag["flag_value"];
		}
		
		return $Flags;
	}
}

?>